using Engine;
using Game;
using GameEntitySystem;

namespace SCIENEW {
    public class SifterDeviceBehavior : EntityDeviceBehavior<ComponentSifter> {
        public SubsystemBlockEntities m_subsystemBlockEntities;

        public SifterDeviceBehavior(Project project) : base(project) {
            m_subsystemBlockEntities = project.FindSubsystem<SubsystemBlockEntities>(true);
        }

        public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner) {
            ComponentBlockEntity componentBlockEntity = m_subsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
            if (componentBlockEntity == null) {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("筛子貌似坏了，重新放置一下吧~", Color.White, false, false);
                return false;
            }
            ComponentSifter componentSifter = componentBlockEntity.Entity.FindComponent<ComponentSifter>();
            if (componentSifter == null) {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("筛子貌似坏了，重新放置一下吧~", Color.White, false, false);
                return false;
            }

            IInventory inventory = componentMiner.Inventory;
            int activeSlotIndex = componentMiner.Inventory.ActiveSlotIndex;
            int blockValue = componentMiner.ActiveBlockValue;
            if (ComponentInventoryBase.AcquireItems(componentSifter, blockValue, 1) == 0) {//筛子接受物品
                inventory.RemoveSlotItems(activeSlotIndex, 1);//移除手上的物品
                return true;
            }
            else {
                return componentSifter.Sift();
            }
            return false;
        }
    }
}